<?php
/**
 * Copyright (C) 2008 PHP Overmind - http://code.google.com/p/string-helper/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 
/** returns the string between 2 strings
*   $text - the string to search in
*   $separatorStart - the substring that precedes the searched string
*   $separatorStart - the substring that succeeds the searched string
*   $after - if specified it looks for the string according to the rules only after the $after is encountered
*   returns - the substring in between; if the $after variable is specified it returns a substring only if after is encountered.
**/
function strInBetween($text, $separatorStart, $separatorEnd, $after = null)
{
    $afterIndex = 0;

    if ($after != null)
	{
        $pos = strpos($text, $after);
		if ($pos !== false)
			$afterIndex = $pos;
	}
		
	$start = -1;
	$pos = strpos($text, $separatorStart, $afterIndex);
	if ($pos !== false)
		$start = $pos;
	
	$end = -1;	
	$pos = strpos($text, $separatorEnd, $start + strlen($separatorStart));
	if ($pos !== false)
		$end = $pos;		
	
//	echo '[' . $start . ' | ' . $end . ']';
	
	if ($start < 0 && $end < 0)
		return "";
	else
		return substr($text, $start + strlen($separatorStart), $end - ($start + strlen($separatorStart)));
}

?>